package com.cw.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cw.common.api.Response;
import com.cw.common.method.CommonMethod;
import com.cw.entity.MessMainInfo;
import com.cw.mapper2.*;
import com.cw.service.IMessMainInfoService;
import com.cw.util.CirculateMsg;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

@Service
public class CirculateInfolmpl extends ServiceImpl<MessMainInfoMapper, MessMainInfo> implements IMessMainInfoService {

    private CirculateMsg circulateMsg = new CirculateMsg();
    @Autowired
    private MessMainInfoMapper messMainInfoMapper;


    private static ReentrantLock lock = new ReentrantLock();

    @Override
    public Response sendYGMsg() {
        new Thread(() -> {
            try {
                YGMsgProcess();
            } catch (Exception ex) {
            }
        }).start();
        return Response.success("EMR信息发送成功");
    }

    /**
     * 每五分钟循环发送院感信息
     */
    public void YGMsgProcess() {
        try {
            List<MessMainInfo> messMainInfoList = messMainInfoMapper.getCirculateMsg();
            List<MessMainInfo> updateMessInfoList = new ArrayList<>();
            //遍历每条内容
            //获取当前的日期
            Date date = new Date();
            //设置日期格式
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //获取String类型的时间
            String time = df.format(date);
            for (MessMainInfo messMainInfo : messMainInfoList) {
                // 设定用户
                circulateMsg.setUserName("S01");
                // 设定密码
                circulateMsg.setPassword("123456");
                // 设定通道池
                circulateMsg.setQueueManagerName("QMGR.P01");
                // 设定消息频道
                circulateMsg.setChannel("PS35035");
                // 取消息主题
                circulateMsg.setMsgTitle(messMainInfo.getMessTitle());
                //赋值消息内容
                circulateMsg.setMsgContent(messMainInfo.getMessNr());
                //取消息类型
                circulateMsg.getMsgType();
                // 设定消息内容
                circulateMsg.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                // 设定发送者工号
                circulateMsg.setSenderCode(messMainInfo.getOperCode());
                // 设定发送者姓名
                circulateMsg.setSenderName(messMainInfo.getOperName());
                //{    字段赋空
                //患者姓名
                circulateMsg.setPatName("");
                //患者索引号
                circulateMsg.setPatIndexNo("");
                //创建人工号
                circulateMsg.setReciveCode("");
                //创建人姓名
                circulateMsg.setReciveName("");
                //床位号
                circulateMsg.setBedNum("");
                //病区代码
                circulateMsg.setWardCode("");
                //病区名称
                circulateMsg.setWardName("");
                circulateMsg.setHospDistrictCode("");
                circulateMsg.setHostDistrictName("");
                   //  }
                String isAll = messMainInfo.getIfAlluser();
                String dept = messMainInfo.getSendDeptcode();
                String oneoper = messMainInfo.getSendOneoper();
                if (oneoper != null && !oneoper.isEmpty()) {
                    // 消息主题
                    circulateMsg.setMsgTitle(messMainInfo.getMessTitle().concat("(个人)"));
                    circulateMsg.setMessageType("2");
                    //接收者工号，单独发送给某人
                    circulateMsg.setReciveName(messMainInfo.getSendOneoper());
                } else if (oneoper == null && dept != null && !dept.isEmpty()) {
                    // 消息主题
                    circulateMsg.setMsgTitle(messMainInfo.getMessTitle().concat("(科室)"));
                    circulateMsg.setDeptCode(messMainInfo.getSendDeptcode());
                    circulateMsg.setDeptName(messMainInfo.getDeptName());
                    circulateMsg.setMessageType("1");
                } else if (oneoper == null && dept == null && isAll != null && !isAll.isEmpty()) {
                    // 消息主题
                    circulateMsg.setMsgTitle(messMainInfo.getMessTitle().concat(("(全院)")));
                    circulateMsg.setMessageType("0");
                }
                String xmlString = circulateMsg.createXMLString(circulateMsg);
                //拿到返回的msgId
                String msgId = circulateMsg.putMsg(xmlString);
                //已发送
                messMainInfo.setMqSendFlag("1");
                //发送时间
                messMainInfo.setSendTime(time);
                if (!msgId.equals("error")) {
                    //接收状态 “1”已接收
                    messMainInfo.setMqReceiveFlag("1");
                } else {
                    //接收状态 “0”未接收
                    messMainInfo.setMqReceiveFlag("0");
                }
                updateMessInfoList.add(messMainInfo);
            }
            if(CollectionUtils.isNotEmpty(updateMessInfoList))
            {
                Date currDate = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                List<HashMap<String, Object>> updateMap = new ArrayList<>();
                updateMessInfoList.forEach(item->{

                    HashMap<String, Object> param = new HashMap<>(3);
                    param.put("XH", item.getXh());
                    param.put("MQ_SEND_FLAG", item.getMqSendFlag());
                    param.put("MQ_RECEIVE_FLAG", item.getMqReceiveFlag());
                    param.put("MQ_SEND_TIME", sdf.format(currDate));
                    updateMap.add(param);
                });
                //批量修改
                messMainInfoMapper.updateMessInfo(updateMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

